package com.jerry.template.module.mainpage.shouye.duanzi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.jerry.template.R;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.CommDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommenticonListAdapter<T> extends BaseAdapter {

    private List<CommDetail.DataBean.RecentCommentsBean> recentCommentsBeen = new ArrayList<CommDetail.DataBean.RecentCommentsBean>();

    private Context context;
    private LayoutInflater layoutInflater;
    private ViewHolder viewHolder;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    public CommenticonListAdapter(Context context, List<CommDetail.DataBean.RecentCommentsBean> recentCommentsBeen) {
        this.context = context;
        this.recentCommentsBeen = recentCommentsBeen;
        this.layoutInflater = LayoutInflater.from(context);
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
    }

    @Override
    public int getCount() {
        return recentCommentsBeen.size();
    }

    @Override
    public CommDetail.DataBean.RecentCommentsBean getItem(int position) {
        return recentCommentsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.commenticon_list, null);
            viewHolder = new ViewHolder();

            ButterKnife.bind(viewHolder, convertView);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CommDetail.DataBean.RecentCommentsBean recent = getItem(position);

        viewHolder.userCommName.setText(recent.getUser_name()+"");

        viewHolder.btnCommDigg.setText(recent.getDigg_count()+"");

        viewHolder.duanziCommContent.setText(recent.getText()+"");

        imageLoader.displayImage(
                recent.getAvatar_url(),
                viewHolder.userCommHead,
                options);

        return convertView;
    }

    protected class ViewHolder {
        @BindView(R.id.user_comm_head)
        CircleImageView userCommHead;
        @BindView(R.id.user_comm_name)
        TextView userCommName;
        @BindView(R.id.btn_comm_digg)
        Button btnCommDigg;
        @BindView(R.id.duanzi_comm_content)
        TextView duanziCommContent;
    }
}
